function [VAR var sd] = var_growth(id,year,X,w)

[h1,h2,ii] = unique(id);
[h1,h2,jj] = unique(year);

N = max(ii);
T = max(jj);

bigX = zeros(N,T);
bigX(sub2ind([N T],ii,jj)) = X;

bigO = zeros(N,T);
bigO(sub2ind([N T],ii,jj)) = 1;
counts = sum(bigO,2);

mX = sum(bigX,2)./sum(bigO,2);
demeanX = (bigX-repmat(mX,1,T)).*bigO;



Y = reshape(demeanX,N,T);

t = counts - ones(size(counts,1),1);
tt = repmat(t,1,T);
TT = reshape(tt,N,T);

Y = Y./sqrt(TT);



W = zeros(N,T);
W(sub2ind([N T],ii,jj)) = w;

y = W'*Y;
VAR = y*y'

var = diag(VAR);
sd = sqrt(var);
